.section .text
.type insertion_sort, @function
.globl insertion_sort
insertion_sort:
	pushl %ebp
	movl %esp, %ebp

	# apply 12-byte to store local variables
	subl $0xc, %esp
	
	# save ECX register
	pushl %ecx
	
	# initinate ECX register with the second argument which is the array's length
	movl 8(%ebp), %ecx
	decl %ecx
loop1:
	pushl %eax
	pushl %ebx
	pushl %edx
	
	# local variable: j
	movl 8(%ebp), %eax
	movl %eax, -4(%ebp)
	subl %ecx, -4(%ebp)

	# local variable: key
	movl 12(%ebp), %eax
	movl -4(%ebp), %ebx
	movl (%eax, %ebx, 4), %edx
	movl %edx, -8(%ebp)

	popl %edx
	popl %ebx
	popl %eax

	# local variable: i
	pushl %ecx
	movl -4(%ebp), %ecx
	decl %ecx
loop2:
	pushl %eax
	pushl %ebx

	# local variable: temp
	movl 12(%ebp), %eax
	movl (%eax, %ecx, 4), %ebx
	movl %ebx, -12(%ebp)
	
	movl -8(%ebp), %eax
	movl -12(%ebp), %ebx
	cmpl %ebx, %eax
	jg loop2_end

	movl 12(%ebp), %eax
	incl %ecx
	movl %ebx, (%eax, %ecx, 4)
	decl %ecx

	popl %ebx
	popl %eax
	
	loop loop2
	jmp loop1_end
loop2_end:
	movl 12(%ebp), %ebx
	incl %ecx
	movl %eax, (%ebx, %ecx, 4)
	decl %ecx
	
	popl %ebx
	popl %eax
loop1_end:	
	popl %ecx
	loop loop1
end:	
	# restore ECX register
	popl %ecx
	
	movl %ebp, %esp
	pop %ebp
	ret
	